// J. P. Diaz-Sanchez & A. Yerovi
// Better off by law? The Association between a Constitutional Re-form and the Subjective Well-Being of the LGBT community
// Preliminary treatment of the original dataset and variable creation 

// local pathname "XXXXXX"  //Set the path to the directory  
local filename "original_INEC_data.dta"  
use `pathname'`filename'

/*--------  Depuring the initial database --------- */
  
//"Seccion 2 P4"
// S3_P04121 is a string var.
gen S3_OTHER = .
replace S3_OTHER = 1 if S3_P04121 != ""
replace S3_OTHER = . if S3_P04121 == "NO RESPONDE"
replace S3_OTHER = 0 if S3_P04121 == ""

// Does not belong to the economically active population
drop if N_OCUPADOS == 2

// Age
sum  S1_P01, de
replace S1_P01 =. if S1_P01 >= 98
gen AGE = S1_P01
gen AGE2 = AGE*AGE

//  Is the individual a member of a LGBT group
gen M = .
replace M = 0 if IORG == 2
replace M = 1 if IORG == 1

// Sex at birth (SB)
// We code SB == 1 if born female
sum S1_P03, de
gen SB = .
replace SB = 0 if S1_P03 == 1
replace SB = 1 if S1_P03 == 2


// 9 is "not answered"
replace S6_P05 = . if S6_P05 == 9


// Occupation
// 12 is "not answered" 
gen OCUPACION = S6_P05
replace S5_P03 =. if S5_P03 == 12   


gen EDUC = S5_P03

// 8 is "not answered" 
sum S1_P02, de
replace S1_P02 =. if S1_P02 == 8
gen M_STATUS = S1_P02	


// Sexual inclination / orientation
// 7 is "not answered"
replace S1_P04 =. if S1_P04 == 7		
gen SEX_INCLIN = S1_P04

// Is the individual (or has ever been) a sexual worker
gen SEX_WORK = . 
replace SEX_WORK = 1 if S6_P09 == 1
replace SEX_WORK = 0 if S6_P09 == 2

// Type of job/charge
gen JOB_CHARGE = S6_P05

// Monthly income
sum S6_P07
replace S6_P07 = . if S6_P07 == 99999
gen INCOME = ln(S6_P07+1) 

// Has kids/offspring
gen KIDS =.
replace KIDS = 1 if S1_P06 == 1
replace KIDS = 0 if S1_P06 == 2

// Born in the place currently living
gen B_THERE = 0
replace B_THERE = 1 if S1_P12 == 1

// Born in a big or metripolitan city (Quito or Guayaquil)
// Variables are imported with labels
gen BIG_CITY = 0
replace BIG_CITY = 1 if  UG_CIUD == 170150
replace BIG_CITY = 1 if  UG_CIUD == 90150


/* -------- WB and Discrimination indexes --------- */

local WBV S3_P041  S3_P042  S3_P043  S3_P044  S3_P045  S3_P046  S3_P047  S3_P048  S3_P049  S3_P0410 
foreach x in `WBV'{
	replace `x' = 0 if `x' == 2
}

gen S = (11-S3_P041-S3_P042-S3_P043-S3_P044-S3_P045-S3_P046-S3_P047-S3_P048-S3_P049-S3_P0410-S3_OTHER)*100/11

//"Seccion 2 P2"

local DF S3_P02A1  S3_P02A2  S3_P02A3  S3_P02A4  S3_P02A5  S3_P02A6  S3_P02A7  S3_P02B1  S3_P02B2  S3_P02B3  S3_P02B4  S3_P02B5  S3_P02C1  S3_P02C2 S3_P02C3  S3_P02C4  S3_P02C5  S3_P02C6  S3_P02C7  S3_P02C8  S3_P02D1  S3_P02D2  S3_P02D3  S3_P02D4  S3_P02D5

foreach x in `DF'{
	replace `x' = 0 if `x' == 2
}

gen DISCR_FAM = S3_P02A1 + S3_P02A2 + S3_P02A3 + S3_P02A4 + S3_P02A5 + S3_P02A6 + S3_P02A7 + S3_P02B1 + S3_P02B2 + S3_P02B3 + S3_P02B4 + S3_P02B5 + S3_P02C1 + S3_P02C2 +S3_P02C3 + S3_P02C4 + S3_P02C5 + S3_P02C6 + S3_P02C7 + S3_P02C8 + S3_P02D1 + S3_P02D2 + S3_P02D3 + S3_P02D4 + S3_P02D5 
gen DISCR_FAM_I = (DISCR_FAM/25)*100

//"Seccion 2 P3"
local DS _V19 _V26 _V33 _V40 _V47 _V54 _V75 _V82 _V89 _V96 _V103 _V110 _V117 _V124 _V129 _V134 _V139 _V144 _V149 _V154 _V159 _V164 _V169

foreach x in `DS'{
	replace `x' = 0 if `x' == 2
}

gen DISCR_SOCIAL = _V19 + _V26 + _V33 + _V40 + _V47 + _V54 + _V75 + _V82 + _V89 + _V96 + _V103 + _V110 + _V117 + _V124 + _V129 + _V134 + _V139 + _V144 + _V149 + _V154 + _V159 + _V164 + _V169
gen DISCR_SOCIAL_I = (DISCR_SOCIAL/23)*100

gen DISCR = (DISCR_FAM + DISCR_SOCIAL)*100/48


/* --------- Weighted Indexes ----------- */ 

/* --------- D_W1 ----------- */ 

gen D_W1 = S3_P02A1*0.02739726 + S3_P02A2*0.02739726 + S3_P02A3*0.02739726 + S3_P02A4*0.02739726 + S3_P02A5*0.02739726 + S3_P02A6*0.02739726 + S3_P02A7*0.02739726 + S3_P02B1*0.02739726 + S3_P02B2*0.02739726 + S3_P02B3*0.02739726 + S3_P02B4*0.02739726 + S3_P02B5*0.02739726 + S3_P02C1*0.02739726 + S3_P02C2*0.02739726 + S3_P02C3*0.02739726 + S3_P02C4*0.02739726 + S3_P02C5*0.02739726 + S3_P02C6*0.02739726 + S3_P02C7*0.02739726 + S3_P02C8*0.02739726 + S3_P02D1*0.02739726 + S3_P02D2*0.02739726 + S3_P02D3*0.02739726 + S3_P02D4*0.02739726 + S3_P02D5*0.02739726 + _V19*0.01369863 + _V26*0.01369863 + _V33*0.01369863 + _V40*0.01369863 + _V47*0.01369863 + _V54*0.01369863 + _V75*0.01369863 + _V82*0.01369863 + _V89*0.01369863 + _V96*0.01369863 + _V103*0.01369863 + _V110*0.01369863 + _V117*0.01369863 + _V124*0.01369863 + _V129*0.01369863 + _V134*0.01369863 + _V139*0.01369863 + _V144*0.01369863 + _V149*0.01369863 + _V154*0.01369863 + _V159*0.01369863 + _V164*0.01369863 + _V169*0.01369863

corr S3_P02A1 S3_P02A2 S3_P02A3 S3_P02A4 S3_P02A5 S3_P02A6 S3_P02A7 S3_P02B1 S3_P02B2 S3_P02B3 S3_P02B4 S3_P02B5 S3_P02C1 S3_P02C2 S3_P02C3 S3_P02C4 S3_P02C5 S3_P02C6 S3_P02C7 S3_P02C8 S3_P02D1 S3_P02D2 S3_P02D3 S3_P02D4 S3_P02D5 _V19 _V26 _V33 _V40 _V47 _V54 _V75 _V82 _V89 _V96 _V103 _V110 _V117 _V124 _V129 _V134 _V139 _V144 _V149 _V154 _V159 _V164 _V169
mat CORR = r(C)

mata: 
R = st_matrix("CORR")
P = (0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, .027, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.027397, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699, 0.013699)
PT = P'
V0 = P*R
V_sq = V0*P'
st_local("W_STD_1", strofreal(V_sq[1,1]))
end

local V_1 = sqrt(`W_STD_1')
replace D_W1 = D_W1/`V_1'

/* --------- D_W2 ----------- */ 

gen D_W2 = S3_P02A1*0.008547009 + S3_P02A2*0.008547009 + S3_P02A3*0.017094017 + S3_P02A4*0.017094017 + S3_P02A5*0.034188034 + S3_P02A6*0.034188034 + S3_P02A7*0.017094017 + S3_P02B1*0.034188034 + S3_P02B2*0.017094017 + S3_P02B3*0.008547009 + S3_P02B4*0.017094017 + S3_P02B5*0.017094017 + S3_P02C1*0.034188034 + S3_P02C2*0.017094017 + S3_P02C3*0.008547009 + S3_P02C4*0.008547009 + S3_P02C5*0.034188034 + S3_P02C6*0.034188034 + S3_P02C7*0.008547009 + S3_P02C8*0.008547009 + S3_P02D1*0.034188034 + S3_P02D2*0.034188034 + S3_P02D3*0.034188034 + S3_P02D4*0.034188034 + S3_P02D5*0.034188034 + _V19*0.008547009 + _V26*0.017094017 + _V33*0.017094017 + _V40*0.017094017 + _V47*0.017094017 + _V54*0.017094017 + _V75*0.008547009 + _V82*0.008547009 + _V89*0.034188034 + _V96*0.034188034 + _V103*0.034188034 + _V110*0.034188034 + _V117*0.034188034 + _V124*0.034188034 + _V129*0.034188034 + _V134*0.017094017 + _V139*0.017094017 + _V144*0.017094017 + _V149*0.008547009 + _V154*0.008547009 + _V159*0.008547009 + _V164*0.008547009 + _V169*0.008547009

corr S3_P02A1 S3_P02A2 S3_P02A3 S3_P02A4 S3_P02A5 S3_P02A6 S3_P02A7 S3_P02B1 S3_P02B2 S3_P02B3 S3_P02B4 S3_P02B5 S3_P02C1 S3_P02C2 S3_P02C3 S3_P02C4 S3_P02C5 S3_P02C6 S3_P02C7 S3_P02C8 S3_P02D1 S3_P02D2 S3_P02D3 S3_P02D4 S3_P02D5 _V19 _V26 _V33 _V40 _V47 _V54 _V75 _V82 _V89 _V96 _V103 _V110 _V117 _V124 _V129 _V134 _V139 _V144 _V149 _V154 _V159 _V164 _V169
mat CORR = r(C)

mata: 
R = st_matrix("CORR")
P = (0.008547009, 0.008547009, 0.017094017, 0.017094017, 0.034188034, 0.034188034, 0.017094017, 0.034188034, 0.017094017, 0.008547009, 0.017094017, 0.017094017, 0.034188034, 0.017094017, 0.008547009, 0.008547009, 0.034188034, 0.034188034, 0.008547009, 0.008547009, 0.034188034, 0.034188034, 0.034188034, 0.034188034, 0.034188034, 0.008547009, 0.017094017, 0.017094017, 0.017094017, 0.017094017, 0.017094017, 0.008547009, 0.008547009, 0.034188034, 0.034188034, 0.034188034, 0.034188034, 0.034188034, 0.034188034, 0.034188034, 0.017094017, 0.017094017, 0.017094017, 0.008547009, 0.008547009, 0.008547009, 0.008547009, 0.008547009)
PT = P'
V0 = P*R
V_sq = V0*P'
st_local("W_STD_2", strofreal(V_sq[1,1]))
end 

local V_2 = sqrt(`W_STD_2')
replace D_W2 = D_W2/`V_2'

/* --------- D_W3 ----------- */ 

gen D_W3 = S3_P02A1*0.010989011 + S3_P02A2*0.010989011 + S3_P02A3*0.021978022 + S3_P02A4*0.021978022 + S3_P02A5*0.043956044 + S3_P02A6*0.043956044 + S3_P02A7*0.021978022 + S3_P02B1*0.043956044 + S3_P02B2*0.021978022 + S3_P02B3*0.010989011 + S3_P02B4*0.021978022 + S3_P02B5*0.021978022 + S3_P02C1*0.043956044 + S3_P02C2 *0.021978022 + S3_P02C3*0.010989011 + S3_P02C4*0.010989011 + S3_P02C5*0.043956044 + S3_P02C6*0.043956044 + S3_P02C7*0.010989011 + S3_P02C8*0.010989011 + S3_P02D1*0.043956044 + S3_P02D2*0.043956044 + S3_P02D3*0.043956044 + S3_P02D4*0.043956044 + S3_P02D5*0.043956044 + _V19*0.005494505 + _V26*0.010989011 + _V33*0.010989011 + _V40*0.010989011 + _V47*0.010989011 + _V54*0.010989011 + _V75*0.005494505 + _V82*0.005494505 + _V89*0.021978022 + _V96*0.021978022 + _V103*0.021978022 + _V110*0.021978022 + _V117*0.021978022 + _V124*0.021978022 + _V129*0.021978022 + _V134*0.010989011 + _V139*0.010989011 + _V144*0.010989011 + _V149*0.005494505 + _V154*0.005494505 + _V159*0.005494505 + _V164*0.005494505 + _V169*0.005494505

corr S3_P02A1 S3_P02A2 S3_P02A3 S3_P02A4 S3_P02A5 S3_P02A6 S3_P02A7 S3_P02B1 S3_P02B2 S3_P02B3 S3_P02B4 S3_P02B5 S3_P02C1 S3_P02C2 S3_P02C3 S3_P02C4 S3_P02C5 S3_P02C6 S3_P02C7 S3_P02C8 S3_P02D1 S3_P02D2 S3_P02D3 S3_P02D4 S3_P02D5 _V19 _V26 _V33 _V40 _V47 _V54 _V75 _V82 _V89 _V96 _V103 _V110 _V117 _V124 _V129 _V134 _V139 _V144 _V149 _V154 _V159 _V164 _V169
mat CORR = r(C)

mata: 
R = st_matrix("CORR")
P = (0.010989011, 0.010989011, 0.021978022, 0.021978022, 0.043956044, 0.043956044, 0.021978022, 0.043956044, 0.021978022, 0.010989011, 0.021978022, 0.021978022, 0.043956044, 0.021978022, 0.010989011, 0.010989011, 0.043956044, 0.043956044, 0.010989011, 0.010989011, 0.043956044, 0.043956044, 0.043956044, 0.043956044, 0.043956044, 0.005494505, 0.010989011, 0.010989011, 0.010989011, 0.010989011, 0.010989011, 0.005494505, 0.005494505, 0.021978022, 0.021978022, 0.021978022, 0.021978022, 0.021978022, 0.021978022, 0.021978022, 0.010989011, 0.010989011, 0.010989011, 0.005494505, 0.005494505, 0.005494505, 0.005494505, 0.005494505)
PT = P'
V0 = P*R
V_sq = V0*P'
st_local("W_STD_3", strofreal(V_sq[1,1]))
end 

local V_3 = sqrt(`W_STD_3')
replace D_W3 = D_W3/`V_3'

/* --------- S_W1 ----------- */ 

gen S_W1 = 1 - (S3_P041*0.0625 + S3_P042*0.0625 + S3_P043*0.0625 + S3_P044*0.0625 + S3_P045*0.0625 + S3_P046*0.0625 + S3_P047*0.0625 + S3_P048*0.250 + S3_P049*0.250 + S3_P0410*0.0625)

corr S3_P041  S3_P042  S3_P043  S3_P044  S3_P045  S3_P046  S3_P047  S3_P048  S3_P049  S3_P0410 
mat CORR = r(C)

mata:
R = st_matrix("CORR")
P = (0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.250, 0.250, 0.0625)
PT = P'
V0 = P*R
V_sq = V0*P'
st_local("W_STD_4", strofreal(V_sq[1,1]))
end

local V_4 = sqrt(`W_STD_4')
replace S_W1 = S_W1/`V_4'

/* --------- S_W2 ----------- */ 

gen S_W2 = 1 - (S3_P041*0.0526 + S3_P042*0.0526 + S3_P043*0.0526 + S3_P044*0.0526 + S3_P045*0.1053 + S3_P046*0.1053 + S3_P047*0.1053 + S3_P048*0.2105 + S3_P049*0.2105 + S3_P0410*0.0526)


corr S3_P041  S3_P042  S3_P043  S3_P044  S3_P045  S3_P046  S3_P047  S3_P048  S3_P049  S3_P0410 
mat CORR = r(C)

mata:
R = st_matrix("CORR")
P = (0.0526, 0.0526, 0.0526, 0.0526, 0.1053, 0.1053, 0.1053, 0.2105, 0.2105, 0.0526)
PT = P'
V0 = P*R
V_sq = V0*P'
st_local("W_STD_5", strofreal(V_sq[1,1]))
end

local V_5 = sqrt(`W_STD_5')
replace S_W2 = S_W2/`V_5'


// Not needed

drop S1_P01 S1_P051 S1_P052 S1_P053 S1_P054 S1_P055 S1_P056 S1_P057 S1_P06 S1_P07 S1_P081 S1_P082 S1_P083 S1_P084 S1_P0841 S1_P09 S1_P091 S1_P10 S1_P11 S1_P111 S1_P112 S1_P113 N_S1P11123 S1_P12 S1_P121 S1_P122 S1_P13 S1_P131 S1_P14 S2_P01 S2_P011 S2_P021 S2_P022 S2_P023 S2_P024 S2_P025 S2_P026 _V1 _V2 _V3 _V4 _V5 _V6 _V7 _V8 _V9 _V10 _V11 _V12 _V13 _V14 _V15 _V16 _V17 _V18 S3_P02A1 S3_P02A2 S3_P02A3 S3_P02A4 S3_P02A5 S3_P02A6 S3_P02A7 S3_P02B1 S3_P02B2 S3_P02B3 S3_P02B4 S3_P02B5 S3_P02C1 S3_P02C2 S3_P02C3 S3_P02C4 S3_P02C5 S3_P02C6 S3_P02C7 S3_P02C8 S3_P02D1 S3_P02D2 S3_P02D3 S3_P02D4 S3_P02D5  _V20 _V21 _V22 _V23 _V24 _V25  _V27 _V28 _V29 _V30 _V31 _V32  _V34 _V35 _V36 _V37 _V38 _V39  _V41 _V42 _V43 _V44 _V45 _V46  _V48 _V49 _V50 _V51 _V52 _V53  _V55 _V56 _V57 _V58 _V59 _V60 _V61 _V62 _V63 _V64 _V65 _V66 _V67 _V68 _V69 _V70 _V71 _V72 _V73 _V74  _V76 _V77 _V78 _V79 _V80 _V81  _V83 _V84 _V85 _V86 _V87 _V88  _V90 _V91 _V92 _V93 _V94 _V95  _V97 _V98 _V99 _V100 _V101 _V102  _V104 _V105 _V106 _V107 _V108 _V109  _V111 _V112 _V113 _V114 _V115 _V116  _V118 _V119 _V120 _V121 _V122 _V123  S3_OBS S4_P011 S4_P012 S4_P013 S4_P014 S4_P015 S4_P016 S4_P017 S4_P018 S4_P019 S4_P0110 S4_P02 S4_P03 S4_P031 S4_P04 S4_P041 S4_P051 S4_P052 S4_P053 S4_P054 S4_P055 S4_P056 S4_P057 S4_P058 S4_P06 S4_P061 S4_P07 S4_P08 S4_P09 S4_P10 S4_P111 S4_P112 S4_P113 S4_P114 S4_P115 S4_P116 S4_P117 S4_P1171 S4_P12 S4_P121 S4_P13 S4_OBS S5_P01 S5_P02 S5_P021 S5_P04 S5_P05 S5_P051 S5_P06  _V125 _V126 _V127 _V128  _V130 _V131 _V132 _V133  _V135 _V136 _V137 _V138  _V140 _V141 _V142 _V143  _V145 _V146 _V147 _V148  _V150 _V151 _V152 _V153  _V155 _V156 _V157 _V158  _V160 _V161 _V162 _V163  _V165 _V166 _V167 _V168  _V170 _V171 _V172 _V173 S5_P081 S5_P082 S5_P083 S5_P084 S5_P085 S5_P086 S5_P087 S5_P088 S5_P089 S5_P0891 S5_P09 S5_OBS S6_P01 S6_P02 S6_P021 S6_P03CIIU S6_P04CIUO  S6_P06 S6_P07 S6_P081 S6_P082 S6_P083 S6_P084 S6_P085 S6_P086 S6_P087 S6_P088 S6_P089 S6_P0810 S6_P0811 S6_P08111 S6_P09 S6_P101 S6_P102 S6_P103 S6_P104 S6_P105 S6_P1051 S7_P01 _V174 _V175 _V176 _V177 _V178 _V179 _V180 _V181 _V182 _V183 _V184 _V185 _V186 _V187 _V188 _V189 _V200 _V201 _V202 _V203 _V204 _V205 _V206 _V207 _V208 _V209 _V210 _V211 _V212 _V213 S7_P03 S7_P04 S7_P041 S7_P05 S7_P051 S7_P06 S8_P01 S8_P02 S8_P03 S8_P04 S8_P051 S8_P052 S8_P053 S8_P054 S8_P0541 S8_P06 S8_P071 S8_P072 S8_P073 S8_P074 S8_P0741 S8_P08 S8_P081 S8_P09 S8_P091 S8_P092 S8_P10 S8_P111 S8_P112 S8_P113 S8_P114 S8_P115 S8_P1151 S9_P01 S9_P021 S9_P022 S9_P023 S9_P024 S9_P025 S9_P026 S9_P027 S9_P028 S9_P029 S9_P0210 S9_P0211 S9_P0212 S9_P02121 S9_P031 S9_P032 S9_P033 S9_P034 S9_P035 S9_P036 S9_P037 S9_P038 S9_P039 S9_P0310 S9_P0311 S9_P03111 S9_P04 S9_P05 N_CONTROL N_IMPOSICION N_RECHAZO N_VIOLENCIA N_CIR N_CIRV _V214 N_CIIU_3DIG _V215 _V216 N_S3_P03A N_S3_P03B N_S3_P03C N_S3_P03ABC N_S3_P03ABCEDU N_S3_P03ABCLAB N_S3_P03ABCSAL N_S3_P03ABCJUS _V217 _V218 N_S3_P03AEDU N_S3_P03BEDU N_S3_P03CEDU N_S3_P03ALAB N_S3_P03BLAB N_S3_P03CLAB N_S3_P03ASAL N_S3_P03BSAL N_S3_P03CSAL N_S3_P03AJUS N_S3_P03BJUS N_S3_P03CJUS _V219 _V220 _V221 _V222 _V223 _V224 N_CIIU_2DIG N_CIIU_1DIG N_CIUO_3DIG N_CIUO_2DIG N_CIUO_1DIG N_OCUPADOS N_S4_SEGUROS N_S4_OTRO_SEGURO

 save `pathname' "data.dta", replace

